set more off

// Set directories
global rootsoep  //Set path to SOEP-CORE_v33.1_stata_bilingual.dta
global root  //Set path to working folder


 
//Clean the 2008 data
use hhnr persnr hhnrakt yp09 using "$rootsoep/yp.dta", clear
sort persnr 
merge  persnr using "$rootsoep/ypequiv.dta"
drop _merge
rename yp09 HHpatience
tempfile d2008
save `d2008'

//Clean the 2013 data
use hhnr persnr hhnrakt bdp153 using "$rootsoep/bdp.dta", clear
sort persnr 
merge persnr using "$rootsoep/bdpequiv.dta"
drop _merge
rename bdp153 HHpatience
tempfile d2013
save `d2013'

//Add 2016?


// Merge the two data sets
foreach j in 08 13{
use `d20`j'',clear
rename d11106`j' noPersonshh
rename d11105`j' head
rename e11102`j' empstatus
rename d11102ll gender
rename d11104`j' maritalstatus
rename d11107`j' numchildren
rename d11101`j' HHage
rename i11102`j' HHincome
rename chspt`j' childsupport
rename l11101`j' state
rename d11109`j' yearseduc
rename w11102`j' weight

gen year = `j'+2000
keep year hhnr persnr hhnrakt empstatus head noPersonshh gender maritalstatus numchildren HHage HHincome childsupport HHpatience state yearseduc weight
order year, before(hhnr) 
sort year persnr 
save `d20`j'', replace
}
merge year persnr using `d2008'
sort year persnr 
//label language EN 


/*
// Find the patience of the head of household of the original household in 2008
//and apply to all members of current and future households
bys hhnr: gen temp= HHpatience if head==1 & hhnr==hhnrakt & year==2008
bys hhnr: egen dynasticHHpatience= max(temp)
drop temp 
drop if dynasticHHpatience==-1 | dynasticHHpatience==.
*/


//Calculate the Total Number of Children
sort hhnrakt persnr year
gen totalChildren = 0
bys persnr: replace totalChildren = _n == 1 if head ==3
bys hhnrakt: egen totalChildrentemp = total(totalChildren)
replace totalChildren=totalChildrentemp
drop totalChildrentemp


//Create useful Dummies and Variables

gen lincome = ln(HHincome)

generate young = (HHage<=25)
gen mediumyoung = (HHage<=35 & HHage>25)
gen mediumold = (HHage<=45 & HHage>35)
gen old = (HHage>45)
generate isHhhead = (head==1)
gen isMale=(gender==1)


drop if maritalstatus==-1
gen isMarried= (maritalstatus ==1)
gen isSingle= (maritalstatus ==2)
gen isWidowed= (maritalstatus ==3)
gen isDivorced= (maritalstatus ==4)
gen isSeperated= (maritalstatus ==5)

drop if HHpatience==-1 | HHpatience==-5 |HHpatience==.








reg totalChildren HHpatience linc mediumold mediumyoung old [aweight=weight] if year==2008, cluster( hhnrakt)
reg totalChildren HHpatience linc mediumold mediumyoung old isHhhead [aweight=weight] if year==2008, cluster( hhnrakt)
reg totalChildren HHpatience linc mediumold mediumyoung old isMale isHhhead [aweight=weight] if year==2008, cluster( hhnrakt)
reg totalChildren HHpatience linc mediumold mediumyoung old isMale isHhhead isMarried isWidowed isDivorced isSeperated[aweight=weight] if year==2008, cluster( hhnrakt)

x


drop if head >=3 

reg totalChildren HHpatience [aweight=weight] if year==2008 & HHage < 41, cluster( hhnrakt)
outreg2   HHpatience HHage  linc  using  "$root/RegressionOutputs/Table1.tex", tex(frag pr) nolabel nocons keep(HHpatience linc HHage) replace ctitle(totalChildren) dec(3) addtext(Controls,no, Ages, 18-40)

reg totalChildren HHpatience HHage [aweight=weight] if year==2008, cluster( hhnrakt)
outreg2   HHpatience HHage linc  using  "$root/RegressionOutputs/Table1.tex", tex(frag pr) nolabel nocons keep(HHpatience linc HHage) append ctitle(totalChildren) dec(3) addtext(Controls,no, Ages, All)

reg totalChildren HHpatience HHage linc[aweight=weight] if year==2008, cluster( hhnrakt)
outreg2   HHpatience HHage linc  using  "$root/RegressionOutputs/Table1.tex", tex(frag pr) nolabel nocons keep(HHpatience linc HHage) append ctitle(totalChildren) dec(3) addtext(Controls,no , Ages, All)

reg totalChildren HHpatience linc HHage isMale isHhhead isMarried isWidowed isDivorced isSeperated[aweight=weight] if year==2008, cluster( hhnrakt)
outreg2   HHpatience HHage linc  using  "$root/RegressionOutputs/Table1.tex", tex(frag pr) nolabel nocons keep(HHpatience linc HHage) append ctitle(totalChildren) dec(3) addtext(Controls,yes , Ages, All)

reg totalChildren HHpatience linc HHage isMale isHhhead isMarried isWidowed isDivorced isSeperated[aweight=weight] if year==2008 & HHage < 41, cluster( hhnrakt)
outreg2   HHpatience HHage linc  using  "$root/RegressionOutputs/Table1.tex", tex(frag pr) nolabel nocons keep(HHpatience linc HHage) append ctitle(totalChildren) dec(3) addtext(Controls,yes , Ages, 18-40)







reg totalChildren HHpatience mediumyoung mediumold old [aweight=weight] if year==2008, cluster( hhnrakt)
outreg2   HHpatience HHage linc mediumyoung mediumold old  using  "$root/RegressionOutputs/Table2.tex", tex(frag pr) nolabel nocons keep(HHpatience HHage linc mediumyoung mediumold old) replace ctitle(totalChildren) dec(3) addtext(Controls,yes )

reg totalChildren HHpatience mediumyoung mediumold old linc [aweight=weight] if year==2008, cluster( hhnrakt)
outreg2   HHpatience HHage linc mediumyoung mediumold old  using  "$root/RegressionOutputs/Table2.tex", tex(frag pr) nolabel nocons keep(HHpatience HHage linc mediumyoung mediumold old) append ctitle(totalChildren) dec(3) addtext(Controls,yes )

reg totalChildren HHpatience mediumyoung mediumold old linc isMale isHhhead isMarried isWidowed isDivorced isSeperated[aweight=weight] if year==2008, cluster( hhnrakt)
outreg2   HHpatience HHage linc mediumyoung mediumold old  using  "$root/RegressionOutputs/Table2.tex", tex(frag pr) nolabel nocons keep(HHpatience HHage linc mediumyoung mediumold old) append ctitle(totalChildren) dec(3) addtext(Controls,yes )


